package com.cjb.cloudsecurity.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cjb.cloudsecurity.JWT.TokenStoreCache;
import com.cjb.cloudsecurity.userMsg.entity.SysUserEntity;
import com.cjb.cloudsecurity.userMsg.mapper.UserMapper;
import com.cjb.cloudsecurity.userMsg.service.SysUserDetailsService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @Author chenjinbin
 * @Package IntelliJ IDEA
 * @Class UserDetailsServiceImpl
 * @Date 2023-08-20 22:16
 */
@Service
@AllArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {
    /**
     * 日志
     */
    private final static Logger log = LoggerFactory.getLogger(UserDetailsServiceImpl.class);
    /**
     *注入用户信息权限控制类
     */
    @Autowired
    private SysUserDetailsService sysUserDetailsService;
    /**
     * 加载仓库
     */
    @Autowired
    private UserMapper userMapper;
    /**
     * 注入缓存类
     */
    @Autowired
    private TokenStoreCache tokenStoreCache;
    /**
     * 重载用户信息类
     *
     * @Author chenjinbin
     * @Value
     * @Title
     */
    @Override
    public UserDetails loadUserByUsername(String username) {
        /**
         * 重写用户登录 去进行自定义用户名密码认证
         */
        log.info("开始进行登录..");
        SysUserEntity userMsg = userMapper.selectOne(new QueryWrapper<SysUserEntity>().eq("username", username));
        if (userMsg == null) {
            log.info("数据库查不到这个数据");
            throw new UsernameNotFoundException("您输入的用户名或密码错误");
        }
        log.info("打印密码"+userMsg.getPassword());
        UserDetails userDetails = sysUserDetailsService.getUserDetails(userMsg);
        /**
         * 生成token 并且注入缓存
         */
        //String generator = TokenUtils.generator();
        //log.info("打印生成的token{}",generator);
        //tokenStoreCache.saveUser(generator,userMsg);
        return userDetails;
    }
}
